package fons.model.query.fun;

public class FilterFun implements Function {
	public final Function filter;
	public final ListFun list;
	
	public FilterFun() {
		this(null);
	}
	
	public FilterFun(Function filter) {
		this(filter, null);
	}
	
	public FilterFun(Function filter, ListFun list) {
		this.filter = filter;
		this.list = list;
	}
	
	@Override
	public Function apply(Function other) {
		if (filter == null) return new FilterFun(other);
		else return new FilterFun(filter, (ListFun) other.apply());
	}

	@Override
	public Function apply() {
		Function result = new ListFun();
		for (Function fun: list.get()) {
			Function x = fun.apply();
			if (filter.apply(x).apply() == BooleanFun.True)
				result = result.apply(fun.apply());
		}
		return result.apply();
	}

}
